package com.ruoyi.iot.controller;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.minio.MinioService;
import com.ruoyi.iot.domain.CollectPoint;
import com.ruoyi.iot.service.CollectionService;
import io.minio.GetObjectArgs;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/file/")
public class FilePreviewController {
    @Autowired
    private MinioService minioService;
    @Autowired
    private CollectionService collectionService;
    //http://120.46.141.64/prod-api/file/download/{fileId}
    @GetMapping(value = "/download/{fileId}")
    public void download(HttpServletRequest request,
                                           HttpServletResponse response,
                                           @PathVariable("fileId") String fileId) {
        try {
            // fileId必填
            CollectPoint collectPoint = collectionService.selectById(fileId);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, collectPoint.getLeafpicPath());
            minioService.download("youcai",collectPoint.getLeafpicPath(), response.getOutputStream());
        } catch (Exception e) {
        }
    }
}
